﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Secams.model;
using Secams.controller;

namespace Secams.form
{
    public partial class FormResult : Form
    {
        Trials _trials;

        public FormResult(Trials trials)
        {
            //this.Font = AppContext.getDefaultFont(this.Font);
            InitializeComponent();
            _trials = trials;

            cboParticipants.Items.Add("-ALL-");

            SortedSet<string> participants = new SortedSet<string>();

            foreach (TrialProfile trialProfile in _trials.trialProfiles)
            {
                /*StringBuilder sb = new StringBuilder();
                sb.Append(trialProfile.participant.id).Append(";Trial=").Append(trialProfile.trial.trialNum).Append(";")
                    .Append("Scenario=").Append(trialProfile.scenarioName);
                 * */
                participants.Add(trialProfile.participant.id);
            }

            foreach (string participant in participants)
            {
                cboParticipants.Items.Add(participant);
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();

            bool selectedParticipant = false;
            if (cboParticipants.Text.Trim().Length > 0 &&
                !cboParticipants.Text.Equals("-ALL-"))
                selectedParticipant = true;
            

            foreach (TrialProfile trialProfile in _trials.trialProfiles)
            {
                if ( !selectedParticipant 
                    || (selectedParticipant && cboParticipants.Text.Equals(trialProfile.participant.id)))
                {
                    // Print trial & Scenario info
                    sb.Append("[").Append(trialProfile.participant.id).Append(";Trial=").Append(trialProfile.trial.trialNum).Append(";")
                        .Append(" Scenario=").Append(trialProfile.scenarioName).Append("]");
                    sb.Append(" DEC=").Append(trialProfile.decided).Append(" CONF=").Append(trialProfile.decisionConfidence)
                        .Append(" REASON=").Append(trialProfile.decisionReason);
                    // Print label
                    sb.Append(System.Environment.NewLine);
                    sb.Append("id,IP,port,directory,opearation,isThreat,labeledAsThreath");
                    sb.Append(System.Environment.NewLine);
                    foreach (EventProfile eventProfile in trialProfile.trial.eventProfiles)
                    {
                        sb.Append(eventProfile.niEvent.id).Append(",");
                        sb.Append(eventProfile.niEvent.destAddr).Append(",");
                        sb.Append(eventProfile.niEvent.port).Append(",");
                        sb.Append(eventProfile.niEvent.path).Append(",");
                        //sb.Append(eventProfile.niEvent.riskLevel).Append(",");
                        sb.Append(eventProfile.niEvent.operation).Append(",");
                        sb.Append(eventProfile.niEvent.isThreat).Append(",");
                        sb.Append(eventProfile.labeledAsThreath);
                        sb.Append(System.Environment.NewLine);
                        txtOutput.Text = sb.ToString();
                    }
                    sb.Append(System.Environment.NewLine);
                }
            }
        }

    }
}
